package com.bd.demo.ui.page

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.bd.demo.bean.MessageInfo
import com.bd.demo.model.AppViewModel
import com.bd.demo.model.BleViewModel
import com.bd.demo.ui.theme.lightBackground
import com.bd.demo.ui.theme.text12MediumBody
import com.bd.demo.ui.theme.text14MediumOnSecondary
import com.bd.demo.ui.theme.text14MediumTitle
import com.bd.demo.ui.view.ElevationBox
import com.bd.demo.ui.view.SolidButtonWithText
import com.bd.demo.ui.view.TitleBar
import com.d10ng.coroutines.launchIO
import com.d10ng.datelib.toDateStr
import com.google.accompanist.insets.navigationBarsPadding
import com.hailiao.bdproto2p1.bean.basic.CCICA_Bean
import com.hailiao.bdproto2p1.bean.basic.ICA_Type
import com.hailiao.bdproto2p1.worker.BasicWorker2p1

const val MESSAGE_SCREEN = "message_screen"

@Composable
fun MessageScreen(
    navController: NavHostController,
    app: AppViewModel,
    model: BleViewModel
) {

    // 消息列表
    val messages by model.msgList.observeAsState(listOf())

    // UI
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(lightBackground)
            .navigationBarsPadding()
    ) {
        // 标题栏
        TitleBar(
            value = model.bleDeviceInfo?.name?: "",
            onClickBack = {
                navController.navigateUp()
            }
        )
        // 对话列表
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f),
            verticalArrangement = Arrangement.Bottom,
            reverseLayout = true
        ) {
            itemsIndexed(messages) { _, info ->
                MessageItemLayout(info)
            }
        }
        ElevationBox {
            SolidButtonWithText(
                text = "CCICA",
                onClick = {
                    launchIO {
                        val BDICI = BasicWorker2p1.ccica(model.ble, model.getMac(), CCICA_Bean(ICA_Type.READ_LOCAL))
                        app.showSnackBar(BDICI.toReadString())
                    }
                },
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
            )
        }
    }
}

@Composable
private fun MessageItemLayout(msg: MessageInfo) {
    Column(
        modifier = Modifier
            .padding(5.dp)
    ) {
        // 时间
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight(),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = msg.timeLong.toDateStr("yyyy-MM-dd HH:mm:ss.SS"),
                style = text12MediumBody,
                modifier = Modifier
                    .wrapContentSize()
            )
        }
        // 输入消息
        if (msg.isIn) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
                    .padding(end = 30.dp)
                    .background(Color(0xFF9F9FDF), RoundedCornerShape(4.dp))
            ) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(Color(0xFF333399), RoundedCornerShape(4.dp))
                        .padding(8.dp)
                ) {
                    SelectionContainer {
                        Text(text = msg.original, style = text14MediumOnSecondary)
                    }
                }
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp)
                ) {
                    SelectionContainer {
                        Text(text = msg.analysis, style = text14MediumOnSecondary)
                    }
                }
            }
        } else {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
                    .padding(start = 30.dp)
                    .background(Color(0xFFF2F2F2), RoundedCornerShape(4.dp)),
                horizontalAlignment = Alignment.End
            ) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(Color(0xFFFFFFFF), RoundedCornerShape(4.dp))
                        .padding(8.dp)
                ) {
                    SelectionContainer {
                        Text(text = msg.original, style = text14MediumTitle)
                    }
                }
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp)
                ) {
                    SelectionContainer {
                        Text(text = msg.analysis, style = text14MediumTitle)
                    }
                }
            }
        }
    }
}